package com.java.business.report.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.report.bean.T_REPORT_TPL;
import com.java.business.report.service.ReportService;
import com.java.core.bean.LowerCaseKeyMap;
import com.java.core.bean.ResultBean;
import com.java.core.utils.FileUtils;
import com.java.core.utils.IdUtils;
import com.java.core.utils.RequestUtils;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/report")
@Controller
public class ReportController {

	@Autowired
	private ReportService service;

	/**
	 * 报表页面生成方法
	 * 
	 * @param response
	 * @param reportid
	 */
	@RequestMapping("render/{id}")
	public String report(HttpServletResponse response, @PathVariable String id,
			ModelMap model) {
		LowerCaseKeyMap cfg = service.getReport(id);
		String json = JSON.toJSONString(cfg).replaceAll("'", "&dyh;");
		model.put("cfg", json);
		model.put("name", cfg.get("name"));
		return "view/report/report";
	}

	/**
	 * 报表查询后台方法
	 * 
	 * @param response
	 * @param id
	 */
	@RequestMapping("/query")
	public void query(HttpServletResponse response, HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			rb = service.queryForPaigng(RequestUtils.getParams(request));
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getQueryColName")
	public void getQueryColName(HttpServletResponse response, String sql) {
		ResultBean rb = new ResultBean();
		try {
			List<LowerCaseKeyMap> list = service.getQueryColName(sql);
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/export2Excel")
	public void export2Excel(HttpServletResponse response,
			HttpServletRequest request) throws InvalidFormatException,
			IOException {
		service.export2Excel(response, request);
	}

	@RequestMapping("/addTpl")
	public void addTpl(HttpServletResponse response,
			HttpServletRequest request, T_REPORT_TPL tpl) {
		ResultBean rb = new ResultBean();
		try {
			tpl.setTplid(IdUtils.uuid());
			tpl.setTpl(FileUtils.getFileBinary(request, "excel_tpl"));
			service.addTpl(tpl);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/udpateTpl")
	public void udpateTpl(HttpServletResponse response,
			HttpServletRequest request, T_REPORT_TPL tpl) {
		ResultBean rb = new ResultBean();
		try {
			tpl.setTpl(FileUtils.getFileBinary(request, "excel_tpl"));
			service.updateTpl(tpl);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/deleteTpl")
	public void deleteTpl(HttpServletResponse response, T_REPORT_TPL tpl) {
		ResultBean rb = new ResultBean();
		try {
			service.deleteTpl(tpl);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/downloadTpl")
	public void downloadTpl(HttpServletResponse response, T_REPORT_TPL tpl)
			throws IOException {
		service.downloadTpl(response, tpl);
	}
}
